/*FileReader.java Reads Filelines as strokes and returns it back as ArrayList of Strings
Copyright (C) 2013  Dmytro Kostiantynovskyi, HartLi

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class FileReader {
	public static ArrayList<String> getFileStrings(String filename){
		ArrayList<String> lstStrings = new ArrayList<String>();
		try{
			FileInputStream fis = new FileInputStream(filename);
			DataInputStream dis = new DataInputStream(fis);
			BufferedReader br = new BufferedReader(new InputStreamReader(dis));
			String line;
			while ((line = br.readLine()) != null){
				if(line != "" && !line.startsWith("#"))
					lstStrings.add(line);
  			}
  			dis.close();
		}
		catch (Exception ex){
			System.err.println("Can't load a file. Waiting to try again or exit.");
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return lstStrings;
		
	}
	
	public static ArrayList<String> getConvertedToArrayWithIndexKey(String file){
		ArrayList<String> lstList = FileReader.getFileStrings(file);
		ArrayList<String> newList = new ArrayList<String>();
		for(int i=0; i<lstList.size(); i++){
			if(!lstList.get(i).startsWith("[")){
				String[] arrParts = lstList.get(i).split("=");
				int index = Integer.parseInt(arrParts[0]);
				String content = arrParts[1];
				newList.add(index, content); 
			}
		}
		return newList;
	}
}
